/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package authentification.service;

import authentification.Exception.AuthException;
import authentification.dao.DaoUserAuthLocal;
import authentification.entitie.UserAuth;
import utils.Md5Passwd;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 * Service d'Authentification
 * @author loic.vaugeois
 */
@Stateless
public class Authentification {

    @EJB
    private DaoUserAuthLocal daoUserAuth;

    /**
     * Test la validité d'un couple login/password.
     * @param login
     * @param passwd
     * @return UserAuth du login demandé en cas de couple login/password valide.
     * @throws AuthException En cas de couple login/password invalide.
     */
    public UserAuth validUser(String login, String passwd) throws AuthException{
        UserAuth monUser = daoUserAuth.findLogin(login);
        try {
            if (monUser != null && Md5Passwd.testPassword(passwd, monUser.getPasswd())) {
                return monUser;
            }
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Authentification.class.getName()).log(Level.SEVERE, null, ex);
        }
        throw new AuthException("Couple Login/Password invalide.");
    }
}
